package com.yiwenup.leetcode.offer;

/**
 * https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/
 **/
public class No057 {
    /**
     * 执行用时：2 ms, 在所有 Java 提交中击败了93.57%的用户
     * 内存消耗：55.2 MB, 在所有 Java 提交中击败了66.45%的用户
     */
    public int[] twoSum(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int sum = nums[left] + nums[right];
            if (sum == target) return new int[]{nums[left], nums[right]};
            else if (sum < target) left++;
            else right--;
        }
        return new int[0];
    }
}
